<template>
  <div>
    <FormPanel :list="topFilterList" :cols="3" :labelWidth="100" :collapse="false" :isSubmitBtn="false" />
    <FilterTable
      ref="mainTable"
      :columns="tableColumns"
      columnsRef="aduitOrder"
      :datakey="'records'"
      selectionType="single"
      :dataSource="dataSource"
      :isSelectColumn= false
      :onColumnsChange="columns => (this.tableColumns = columns)"
    >
    </FilterTable>

<!--    <span>-->
<!--        订购品种合计： <el-input style="width:150px;" label="订购品种合计" size="medium" :disabled="true" v-model="dataSize"></el-input>-->
<!--      </span>-->
    <FilterTable
      ref="mainTable2"
      :columns="tableColumns2"
      columnsRef="aduitOrder"
      :datakey="'records'"
      selectionType="single"
      :isSelectColumn= false
      :dataSource="dataSource2"
      :onColumnsChange="columns => (this.tableColumns2 = columns)"
    >
    </FilterTable>
    <div class="dialogBottmView">
      <el-button @click="closeDialogTop">返 回</el-button>

    </div>

  </div>
</template>
<script>
import { partbuyStockInItemOEM,auditOrder,aduitPocess } from '@/api/partsmanage/aduitOrder/index';
import { dictionary } from '@/utils/dictMixin';
import { byAllInfo } from '@/api/part/allocation';
import { getStorageList, querySysParam } from '@/api/part/common';
import { getListByPage } from '@/api/claim/factory/AuthQueryFactory';
// import {ddlxArr, ddztArr, ddtblxArr,ddtshArr } from './constant';
// import {math} from '@/utils/math.js';

export default {
  name: 'partBuyItems',
  props: {
    top: {}
  },
  mixins: [dictionary],
  data() {
    return {
      topFilterList: this.createTopFilterList(),
      tableColumns: this.createTableColumns(),
      tableColumns2: this.createTableColumns2(),
      fetchParams: {},
      dataSource: [],
      dataSource2: [],
      storageDic: [
        { text: 'OEM电', value: 'OEM-ELECTIC' },
        { text: 'OEM油', value: 'OEM-OIL' }
      ], // 仓库字典
      visible: false,
      loading: false,
      dataSize:0,


    };
  },
  mounted() {
    // this.translate(this.top.CUSTOMER_CODE);
    // this.getStorage();
    if(this.top.XSDDLX != 30031003){
        this.topFilterList.find(x => x.fieldName == 'YSQDH').hidden = true;
    }
    this.findBuyItems();
  },
  methods: {
    seacherFun(){
      let no = this.topFilterList.find(x => x.fieldName == 'YSQDH').initialValue
      if(no === undefined || no == null || no == ""){
          this.$notify.error('当前预授权单号为空!');
          return;
      }
      getListByPage({
        authNo:no,
        pageNum: 1,
        pageSize: 20,
        current: 1,
        currentPage: 1,
        limit: 20,
        size: 20}).then(res =>{
          if(res.data.records[0] === undefined || res.data.records[0] === null || res.data.records[0] === ""){
              this.$notify.error('当前预授权单号对应的预授权信息为空!');
              return;
          }
          if(res.data.records[0].authId === undefined || res.data.records[0].authId === null || res.data.records[0].authId === ""){
              this.$notify.error('当前预授权单号对应的预授权信息为空!');
              return;
          }
        this.rowData = {
          authId : res.data.records[0].authId
        }
        this.visible = true

      }).catch(res =>{
        this.$notify.error(res.errMsg);

      })
    },
    createTopFilterList() {
      return [
        {
          type: 'INPUT',
          label: '服务商代码',
          fieldName: 'DEALER_CODE',
          initialValue: '',

          disabled: true
        },
        {
          type: 'INPUT',
          label: '服务商名称',
          fieldName: 'DEALER_NAME',
          initialValue: '',

          disabled: true
        },
        {
          type: 'INPUT',
          label: '备件订单号',
          fieldName: 'XSDDHM',
          initialValue: '',
          disabled: true
        },
        {
          type: 'SELECT',
          label: '订单类型',
          initialValue: '',
          fieldName: 'XSDDLX',
          itemList: this.createDictList('3003'),
          disabled: true
        },
        // {
        //   title: '可用余额',
        //   sorter: true,
        //   dataIndex: 'AVAIL_AMOUNT',
        //   precision: 2,
        //   filter: true,
        //   filterType: 'number'
        // },

        {
          type: 'INPUT',
          label: '订单金额',
          fieldName: 'XSYSJE',
          precision: 2,
          disabled: true,
          numberFormat: true
        },
        {
          type: 'DATE',
          fieldName: 'DDTBSJ',
          initialValue: '',
          label: '提报日期',
          dateFormat: 'yyyy-MM-dd',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '订单金额',
          fieldName: 'XSYSJE',
          initialValue: '',
          disabled: true,
          hidden:true
        },
        {
          type: 'INPUT',
          label: '联系人',
          initialValue: '',

          fieldName: 'CONTACT_MAN',
          placeholder: '请输入...',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '联系电话',
          initialValue: '',
          fieldName: 'CONTACT_PHONE',
          placeholder: '请输入...',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '收货详细地址',
          initialValue: '',
          fieldName: 'XXSHDZ',
          placeholder: '请输入...',
          disabled: true
        },
        {
          type: 'SELECT',
          label: '发运方式',
          initialValue: '',
          fieldName: 'DDFYFS',
          itemList: this.createDictList('3024'),
          disabled: true
        },
        {
          type: 'SELECT',
          label: '是否免运费',
          initialValue: '',
          fieldName: 'DDMYF',
          itemList: this.createDictList('1004'),
          disabled: true
        },
        {
          type: 'INPUT',
          label: '预授权单号',
          initialValue: '',
          fieldName: 'YSQDH',
          // disabled: true,
          placeholder:'暂无',
          readonly: true,
          clearable: false,
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun()}></el-button>
              </div>
            );
          }
        },
        {
          type: 'TEXT_AREA',
          label: '备注',
          initialValue: '',
          selfCols: 2,
          maxlength: 1000,
          fieldName: 'XSDDBZ',
          placeholder: '请输入...',
          disabled: true
        },
        {
          type: 'TEXT_AREA',
          label: '审核意见',
          initialValue: '',
          selfCols: 2,
          maxlength: 1000,
          fieldName: 'DDBHBZ',
          placeholder: '请输入...',
          disabled: true
        }

      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          // sorter: true,
          width: 70,
          dataIndex: 'xuhao',
          sorter: true,
          render: props => {
            return <span>{props.row.$index + 1}</span>;
          },
          sorter: true
        },
        {
          title: '备件代码',
          width: 180,
          sorter: true,
          dataIndex: 'BJXMBH',
          showOverflowTooltip: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '备件名称',
          width: 180,
          sorter: true,
          dataIndex: 'BJZWMC',
          showOverflowTooltip: true,
          filter: true,
          filterType: 'input'
        },
        { title: '物料组', dataIndex: 'material',dictItems:this.createDictList('3054'), sorter: true },
        {
          title: '单位',
          width: 110,
          sorter: true,
          dataIndex: 'BJDANW',
          showOverflowTooltip: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '销售包装量',
          sorter: true,
          dataIndex: 'IN_SERVICE_AMOUNT',
          // precision: 2,
          filter: true,
          filterType: 'number',
          min: 0,
          max: Infinity,
          widht: 100,
        },
        {
          title: '服务站进价',
          sorter: true,
          dataIndex: 'JJHDJ',
          precision: 2,
          filter: true,
          numberFormat: true,
          filterType: 'number'
        },
        // {
        //   title: '最小订购量',
        //   sorter: true,
        //   dataIndex: 'IN_SERVICE_AMOUNT',
        //   precision: 2,
        //   filter: true,
        //   filterType: 'number',
        //   min: 0,
        //   max: Infinity,
        //   widht: 100,
        //   hidden:true
        // },

        {
          title: '订购数量',
          sorter: true,
          dataIndex: 'BSTXSL',
          // precision: 2,
          summation: true,
          summationUnit:"(订购数量合计)",
          filter: true,
          filterType: 'number',
          min: 0,
          max: Infinity,
          widht: 130
        },
        {
          title: '订购金额',
          sorter: true,
          dataIndex: 'XSYSJE',
          precision: 2,
          summation: true,
          summationUnit:"(订购金额合计)",
          numberFormat: true,
          filter: true,
          filterType: 'number',
          widht: 130
        }
      ];
    },
    createTableColumns2() {
      return [
        {
          title: '序号',
          // sorter: true,
          width: 70,
          dataIndex: 'xuhao',
          sorter: true,
          render: props => {
            return <span>{props.row.$index + 1}</span>;
          },
          sorter: true
        },
        {
          title: '审核人',
          width: 180,
          sorter: true,
          dataIndex: 'UPDATED_BY',
          showOverflowTooltip: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '审核时间',
          width: 180,
          sorter: true,
          dataIndex: 'AUDIT_TIME',
          showOverflowTooltip: true,
          dateFormat: 'yyyy-MM-dd',
          filter: true,
          sorter: true,
          filterType: 'date-range'
        },
        {
          title: '审核状态',
          sorter: true,
          dataIndex: 'AUDIT_TYPE',
          filter: true,
          render: props => {
            return props.row.AUDIT_TYPE=='10041001'?'审核通过':'审核驳回';
          }
        },
        {
          title: '审核说明',
          sorter: true,
          dataIndex: 'AUDIT_REM',
          filter: true,
        },
      ];
    },

    closeDialogTop(){
       this.$emit('cancel');
    },

    toDecimal(x) {
      var val = Number(x)
      if(!isNaN(parseFloat(val))) {
        val = val.toFixed(2);
      }
      return val;
    },

    async getStorage() {
      const ckRes = await getStorageList();
      const ckDict = ckRes.data.map(x => ({ text: x['STORAGE_NAME'], value: x['STORAGE_CODE'] }));
      this.storageDic = ckDict;
      this.tableColumns.find(x => x.dataIndex == 'STORAGE_CODE').filterItems = this.tableColumns.find(x => x.dataIndex == 'STORAGE_CODE').dictItems = ckDict;
    },
    async findBuyItems() {
      this.topFilterList.map(x => {
        x.initialValue = this.top[x.fieldName];
      });
      let res2 = await aduitPocess({ partOrderId: this.top.ID,aduitCode:'aduitOrder' });
      console.log('this.res22222 ============ :'+ JSON.stringify(res2.data));
      this.dataSource2 = res2.data;
      let res = await partbuyStockInItemOEM({ partOrderId: this.top.ID });
      // console.log('this.form ============ :'+ JSON.stringify(res));
      this.dataSize = res.data.length
      this.dataSource = res.data;
    }
  }
};
</script>
